package ly.gupao.edu.designPattern.prototype.Deep2Seria;

import ly.gupao.edu.designPattern.prototype.Mail;
import ly.gupao.edu.designPattern.prototype.MailTemplate;

import java.io.*;

public class SeriaTest {

    /**
     * 通过序列化和反序列化来实现深克隆对象
     * @param args
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        MailTemplate mailTemplate = new MailTemplate("title");
        Mail mail = new Mail("name", "number", mailTemplate);

        //1、将s1对象序列化为一个数组
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(mail);

        byte mailByte[] = byteArrayOutputStream.toByteArray();


        //2、将字节数组中的内容反序列化为一个Sheep对象
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mailByte);
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

        Mail mailCopy = (Mail) objectInputStream.readObject();

        //测试
        System.out.println(mail.getMailTemplate() == mailCopy.getMailTemplate());



    }
}
